package com.runchinaup.sifitbledome.activity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.runchinaup.sifitbledome.R;
import com.runchinaup.sifitbledome.domain.SyncHeartRateData;

import java.util.ArrayList;

/**
 * Created by liber on 2018/3/16.
 */

public class MainAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<SyncHeartRateData> data;

    public MainAdapter(Context context, ArrayList<SyncHeartRateData> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(context).inflate(R.layout.item_main, parent, false);
        }

        ViewHolder viewHolder = (ViewHolder) ret.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.time = ret.findViewById(R.id.time);
            viewHolder.heartRate = ret.findViewById(R.id.heart_rate);
            ret.setTag(viewHolder);
        }

        SyncHeartRateData syncHeartRateData = data.get(position);

        viewHolder.time.setText(syncHeartRateData.getYear() + "-" + syncHeartRateData.getMonth() + "-"
                + syncHeartRateData.getDay() + "-" + syncHeartRateData.getHour() + "-" + syncHeartRateData.getMinute()
                + "-" + syncHeartRateData.getSecond()
        );
        viewHolder.heartRate.setText(syncHeartRateData.getRate());


        return ret;
    }

    private static class ViewHolder {
        public TextView time;
        public TextView heartRate;
    }
}
